//package com.lzx.adx.report.job;
//
//import cn.hutool.core.collection.CollectionUtil;
//import cn.hutool.http.HttpRequest;
//import cn.hutool.http.HttpUtil;
//import com.alibaba.fastjson.JSONObject;
//import com.lzs.adx.commmon.redis.RedisUtils;
//import com.lzs.adx.commmon.utils.JsonUtils;
//import com.lzs.adx.commmon.utils.Result;
//import com.lzx.adx.report.cache.AccountLocalCache;
//import com.lzx.adx.report.constant.RedisKeyConstant;
//import com.lzx.adx.report.dto.AccountDTO;
//import com.lzx.adx.report.dto.ReportDTO;
//import com.lzx.adx.report.enums.EventTypeEnums;
//import com.lzx.adx.report.model.huawei.request.HwUploadRequest;
//import com.lzx.adx.report.service.ReportService;
//import com.lzx.adx.report.util.HwSecureUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.util.*;
//
//@Component
//@EnableScheduling
//@Slf4j
//public class HwKfyJob {
//
//
//    @Autowired
//    private ReportService reportService;
//
//    @Autowired
//    private RedisUtils redisUtils;
//
//    /**
//     * 华为快分分佣
//     */
//    @Scheduled(cron = "0 */10 * ? * *")
//    public void reportEvent(){
//        log.info("进行华为-快分佣-上报处理-begin");
//        Map<String, Object> stringObjectMap = redisUtils.hgetAll(RedisKeyConstant.HW_KFY);
//        int size = stringObjectMap.size();
//        log.info("进行华为-快分佣-本次需要处理的数量为{}",size);
//        if(CollectionUtil.isNotEmpty(stringObjectMap)){
//            //
//            for(Map.Entry<String,Object> ev:stringObjectMap.entrySet()){
//                Object value = ev.getValue();
//                long eventTime = System.currentTimeMillis();
//                callBackUrl(String.valueOf(value),"6",String.valueOf(eventTime));
//            }
//        }
//        log.info("进行华为-快分佣-上报处理-end");
//    }
//
//
//
//    public void callBackUrl(String requestId, String event_type, String event_time)  {
//        log.info("接收了快分佣给华为 回调信息，请求流水号{},回调类型为{}，回调时间{}",requestId,event_type,event_time);
//
//        //1.把转换结果更新到数据库
//        Map<String,Object> params=new HashMap<>();
//        params.put("bizRequestId",requestId);
//        List<ReportDTO> list = reportService.list(params);
//        ReportDTO reportDTO = list.get(0);;
//        reportDTO.setResultType(event_type);
//        reportDTO.setResultTime(event_time);
//        reportDTO.setUpdateTime(new Date());
//        reportService.updateData(reportDTO);
//
//        AccountDTO accountDTO = AccountLocalCache.getInstance().get(reportDTO.getDspId());
//        log.info("从数据库查询的数据为{}", JsonUtils.toJsonString(reportDTO));
//        //2.回调渠道，调用华为回传接口
//        Long conversion_time=Long.valueOf(reportDTO.getResultTime())/1000;
//        HwUploadRequest hwUploadRequest=new HwUploadRequest();
//        hwUploadRequest.setOaid(reportDTO.getOaid());
//        hwUploadRequest.setConversion_type(EventTypeEnums.getHwCode(reportDTO.getResultType()));
//        hwUploadRequest.setTimestamp(String.valueOf(System.currentTimeMillis()));
//        hwUploadRequest.setCallback(reportDTO.getCallback());
//        hwUploadRequest.setCampaign_id(reportDTO.getCampaignId());
//        hwUploadRequest.setContent_id(reportDTO.getContentId());
//
//        hwUploadRequest.setConversion_time(String.valueOf(conversion_time));
//        hwUploadRequest.setTracking_enabled("1");
//        String paramsBody = JsonUtils.toJsonString(hwUploadRequest);
//
//        log.info("账号为【{}】，给华为请求的参数为{}",reportDTO.getDspId(),paramsBody);
//        Map<String,String> header=new HashMap<>();
//
//        header.put("Authorization", HwSecureUtil.buildAuthorizationHeader(paramsBody,accountDTO.getSspKey()));
//
//        HttpRequest body = HttpUtil.createPost(accountDTO.getSspApiUrl()).addHeaders(header).body(paramsBody);
//        String result = body.execute().body();
//        log.info("账号为【{}】，接收华为转换响应的结果为{}",reportDTO.getDspId(),result);
//        JSONObject jsonObject=JSONObject.parseObject(result);
//        Integer resultCode = jsonObject.getInteger("resultCode");
//        if(resultCode==0){
//            //上报成功了，就把那个回传的给清楚掉
//            log.info("上报成功 id{}",reportDTO.getId());
//            redisUtils.hDel(RedisKeyConstant.HW_KFY,reportDTO.getBizRequestId());
//        }else{
//           log.info("上报失败了 id{},",reportDTO.getDspId());
//        }
//    }
//}
